package com.franklin.java.ui;

import com.franklin.java.icon.PluginIcons;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.RegisterToolWindowTask;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
import com.intellij.ui.content.TabbedContent;
import com.intellij.ui.content.impl.ContentImpl;

import javax.swing.*;

/**
 * 插件窗口
 * @author Franklin
 * @since 2021/9/25 16:38
 */
public class PluginToolWindow {

    private Project project;

    private ToolWindow toolWindow;

    public PluginToolWindow(Project project) {
        this.project = project;
        init();
    }

    private void init(){
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        toolWindow = toolWindowManager.registerToolWindow(RegisterToolWindowTask.closable("Franklin Java Data Helper", PluginIcons.SERVICE));
        hide();
    }

    public static PluginToolWindow getInstance(Project project){
        return ServiceManager.getService(project,PluginToolWindow.class);
    }

    public boolean isShow(){
        return toolWindow.isVisible();
    }

    public void show(){
        if (!isShow()) {
            toolWindow.show(null);
        }
    }

    public void hide(){
        toolWindow.hide(null);
    }

    public void addContent(ToolWindowContent toolWindowContent){
        Content content = toolWindowContent.getContent();
        ContentImpl contentImpl = (ContentImpl) content;
        ContentManager contentManager = toolWindow.getContentManager();
        contentImpl.setManager(contentManager);
        contentManager.addContent(content);
        contentManager.setSelectedContent(content,false);
    }
}
